package com.sdruixinggroup.mondayb2b.ui.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.adapter.OpenAreaAdapter;
import com.sdruixinggroup.mondayb2b.adapter.QuickAdapter;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.OpenAreas;
import com.sdruixinggroup.mondayb2b.models.Province;
import com.sdruixinggroup.mondayb2b.ui.Activities.CityListActivity;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import okhttp3.Call;

/**
 * Created by 00029935 on 2017/12/9.
 */

public class OpenAreaFragment extends BaseFragment {
    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    LFRecyclerView recyclerView;
    @BindView(R.id.loading)
    ProgressBar loading;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private List<OpenAreas.AreasBean> data;
    private OpenAreaAdapter areaAdapter;
    private int areaid;

    @Override
    protected int getInflateLayout() {
        return R.layout.common_fragment_layout;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        areaid=  ((Province.RegionsBean)getActivity().getIntent().getSerializableExtra("Area")).getId();
        Log.e("1234","areaid="+areaid);
    }

    @Override
    protected void setUpView(View view) {

        tvTitle.setText("已开通区域");

        data = new ArrayList<>();
        areaAdapter = new OpenAreaAdapter();
        areaAdapter.setData(data);

        recyclerView.setRefresh(true);
        recyclerView.setLoadMore(false);
        recyclerView.setAdapter(areaAdapter);
        recyclerView.setLFRecyclerViewListener(new LFRecyclerView.LFRecyclerViewListener() {
            @Override
            public void onRefresh() {
                queryData(true);
            }

            @Override
            public void onLoadMore() {
            }
        });

        areaAdapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<OpenAreas.AreasBean>(){
            @Override
            public void OnItemClick(OpenAreas.AreasBean data, int postion) {
                EventBus.getDefault().post(data);
                if(CityListActivity.instance!=null){
                    CityListActivity.instance.finish();
                }
                getActivity().finish();
            }
        });
        queryData(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.ib_back)
    public void onClick() {
        getActivity().finish();
    }

    private void queryData(final Boolean isReflush) {
        if (isFirstIn) {
            isFirstIn = false;
            loading.setVisibility(View.VISIBLE);
        }

        String url = API.AREAS + UserInfoUtil.getInfoToken(getContext())+"&county_id="+areaid;
        HashMap<String, String> heades = new HashMap<>();
        heades.put("Accept", "*/*");

        TypeToken<OpenAreas> typeToken = new TypeToken<OpenAreas>() {
        };

        OkHttpUtils.get().url(url).headers(heades).build().execute(new ObjectCallBack<OpenAreas>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != loading) {
                    loading.setVisibility(View.GONE);
                }

                if (null != recyclerView) {
                    recyclerView.stopLoadMore();
                    recyclerView.stopRefresh(true);
                }
            }

            @Override
            public void onResponse(OpenAreas response, int id) {
                if (isReflush) {
                    data.clear();
                }

                if (null != response) {
                    if (response.getErr_code() == 0) {
                        List<OpenAreas.AreasBean> dataBeen = response.getAreas();
                        if (null != dataBeen && !dataBeen.isEmpty()) {
                            data.addAll(dataBeen);
                        }
                    } else {
                        showMsgToast(response.getErr_msg());
                    }
                }

                if (null != loading) {
                    loading.setVisibility(View.GONE);
                }

                if (null != recyclerView) {
                    recyclerView.stopLoadMore();
                    recyclerView.stopRefresh(true);
                }
            }
        });

    }
}
